package Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import Controller.DBAccess;
import Controller.DBAccessImpl;
import Controller.Ride;
import Controller.RideInfo;
import Model.BankAccountDetails;
import Model.Escort;
import Model.EscortImpl;
import Model.Kid;
import Model.KidImpl;

public class DBAccessTest {

	@Rule
	public ExpectedException thrown = ExpectedException.none();
	
	DBAccess db;
	
	@Before
	public void before() {
		db = new DBAccessImpl();
	}
	
	@Test
	public void testFetchRide() {
		RideInfo ride = db.fetchRide(105136);
		assertEquals(30, ride.getCapacity());
	}
	
	@Test
	public void testStoreAndFetchRide() {
		RideInfo ride = new RideInfo(775632, "The Cobra", Ride.Type.EXTREME, 5, 50, 36, 9.99);
		db.storeRide(ride);
		RideInfo fetched = db.fetchRide(775632);
		assertNotNull(fetched);
		assertEquals(36, fetched.getCapacity());
	}
	
	@Test
	public void testStoreFetchAndDeleteRide() {
		RideInfo ride = new RideInfo(775632, "The Cobra", Ride.Type.EXTREME, 5, 50, 36, 9.99);
		db.storeRide(ride);
		RideInfo fetched = db.fetchRide(775632);
		assertNotNull(fetched);
		assertEquals(36, fetched.getCapacity());
		db.deleteRide(fetched.getID());
		thrown.expect(IllegalArgumentException.class);
		fetched = db.fetchRide(775632);
	}

	@Test
	public void testFetchAllRides() {
		assertEquals(3, db.fetchAllRides().size());
	}
	
	@Test
	public void testStoreKid() {
		Kid kid = KidImpl.newInstance("Dennis", 10, 100, new EscortImpl(new BankAccountDetails()));
		assertNotNull(kid);
		db.storeKid(kid);
		Kid fetched = db.fetchKid(kid.getID());
		assertNotNull(fetched);
		assertEquals(kid.getID(), fetched.getID());
	}
	
	@Test
	public void testEscorts() {
		Escort escort = new EscortImpl(new BankAccountDetails());
		db.storeEscort(escort);
		Escort fetched = db.fetchEscort(escort.getID());
		assertNotNull(fetched);
		assertEquals(escort.getID(), fetched.getID());
		assertEquals(0, fetched.getNumOfKids());
		escort.addKid("Dennis", 10, 100);
		db.storeEscort(escort);
		fetched = db.fetchEscort(escort.getID());
		assertNotNull(fetched);
		assertEquals(1, fetched.getNumOfKids());
		db.deleteEscort(escort.getID());
		thrown.expect(IllegalArgumentException.class);
		db.fetchEscort(escort.getID());
	}
}
